package cn.com.java.controller;

import cn.com.java.core.HttpRes;
import cn.com.java.model.SysMenu;
import cn.com.java.model.SysRole;
import cn.com.java.service.SysMenuService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.micrometer.common.util.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;


@RestController
@RequestMapping("SysMenu")
@RequiredArgsConstructor
public class SysMenuController {

    
    private final SysMenuService sysMenuService;

    /**
     * 查询所有记录
     *
     * @return 返回集合，没有返回空List
     */
    @RequestMapping("list")
    public HttpRes listAll() {
        return HttpRes.OK(sysMenuService.list());
    }


    /**
     * 根据主键查询
     *
     * @param id 主键
     * @return 返回记录，没有返回null
     */
    @RequestMapping("getById")
    public HttpRes getById(Long id) {
        return HttpRes.OK(sysMenuService.getById(id));
    }    
     
    /**
     * 新增，忽略null字段
     *
     * @param sysMenu 新增的记录
     * @return 返回影响行数
     */
    @RequestMapping("insert")
    public HttpRes insert(@RequestBody SysMenu sysMenu) {
        return HttpRes.OK(sysMenuService.getBaseMapper().insert(sysMenu));
    }    
      
    /**
     * 修改，忽略null字段
     *
     * @param sysMenu 修改的记录
     * @return 返回影响行数
     */
    @RequestMapping("update")
    public HttpRes update(@RequestBody SysMenu sysMenu) {
        return HttpRes.OK(sysMenuService.updateById(sysMenu));
    }
    
    /**
     * 删除记录
     *
     * @param id 待删除的记录
     * @return 返回影响行数
     */
    @RequestMapping("delete")
    public HttpRes delete(Long id) {
        return HttpRes.OK(sysMenuService.removeById(id));
    }

    @GetMapping("listByPage")
    public HttpRes listByPage(int pageNum, int pageSize, String menuName) {
        Page<SysMenu> page = new Page<>(pageNum, pageSize);

        QueryWrapper<SysMenu> sysMenuQueryWrapper = new QueryWrapper<>();
        sysMenuQueryWrapper.eq("parent_id",1);

        if (menuName != null && !menuName.isEmpty()) {
            sysMenuQueryWrapper.like("menu_name", menuName.trim());
        }
        page = sysMenuService.page(page, sysMenuQueryWrapper);
        List<SysMenu> list = page.getRecords();
        List<SysMenu> records = sysMenuService.findMenusTree(list);
        page.setRecords(records);
        return HttpRes.OK("查询成功", page);
    }



    @GetMapping("listByParents")
    public HttpRes listByParents() {
        return  HttpRes.OK("查询成功", sysMenuService.listByParents());
    }



    @GetMapping("findSysMensTree")
    public HttpRes findSysMensTree(){
        return HttpRes.OK(sysMenuService.findSysMensTree());
    }

    @GetMapping("findMensBySysRoleId")
    public HttpRes findMensBySysRoleId(Long sysRoleId){
        return HttpRes.OK(sysMenuService.findMensBySysRoleId(sysRoleId));
    }

}